package com.woniu.service.impl;

import com.woniu.dao.LoginDao;
import com.woniu.dao.RbacManagerDao;
import com.woniu.entity.RbacManager;
import com.woniu.entity.ResponseEntity;
import com.woniu.service.LoginService;
import com.woniu.service.RbacRoleService;
import com.woniu.utils.JWTUtils;
import com.woniu.utils.SecurityCryUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginException;
import java.util.Objects;

/**
 * @author:陈涵
 * @Do:
 * @date: 2022/9/5 22:13
 */
@RestController
@RequestMapping("/rbacRole")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao dao;
    @Override
    public ResponseEntity login(String username, String password) throws LoginException {
//        rbacManager.setPassword(MD5Utils.toMD5(rbacManager.getPassword(),rbacManager.getAccount()));
        password = SecurityCryUtils.generateEncryptedPassword(password,username);
        //9381fa227d495438a83431fbf52b7368
        RbacManager one =  dao.findOne(username,password);
        if(Objects.nonNull(one)){
            //不要带上密码
//            one.setPassword("");
            ResponseEntity responseEntity = new ResponseEntity(one);
            //使用工具类生成token返回到客户端
            String token = JWTUtils.sign(one.getAccount(),one.getPassword());
            System.out.println("token"+token);
            responseEntity.putKey("token",token);
            return responseEntity;
        }else{
            throw new LoginException("账号或密码错误！");
        }
    }
}
